<?php
namespace Admin\Controller;
use think\controller;
class ConfigsController extends AuthController
{
        public function index()
        {
            $this->page_title="سېستىما تەڭشەكلىرى";
            $this->configs=M('configs')->order('id asc')->select();
            $this->display();
        }

        //添加
    public function add()
    {
        $page_title="تەڭشەك قوشۇش";
        $this->display();

    }

    //提交表单
    public function addhend()
    {

        $rules=[
            ['title','require','配置标题必须填！'],
            ['name','require','配置英文名称不能为空！'],
            ['value','require','配置值必须填！']
        ];
        if(!M('configs')->validate($rules)->create()) $this->error(M('configs')->getError());
        if(!M('configs')->add(I('post.'))) $this->error(M('configs')->getError());
        //写入到配置文件:
        //1.获取数据
        /**
         * $configs=M('configs')->select();
        $phpData="<?php return".var_export($configs,true)."?>";
        $filename=__ROOT__."/Application/Common/Conf/blog.conf.php";
        file_put_contents($filename,$phpData);
         */
        $this->success('添加成功',U('Configs/index'),1);
    }
    //一批编辑
    public function saveadd()
    {
        $data=I('post.');
        foreach ($data['id'] as $k=>$v){
            M('configs')->where(array('id'=>$v))->save(array('value'=>$data['value'][$k]));
        }
        $this->success('更新成功！',U('Configs/index'),1);
    }
    //通过 Ajax编辑
    public function bianji($id)
    {
        if(M('configs')->where(array('id'=>I('post.id')))->save(array('value'=>I('post.value')))){
            $data=['status'=>1,'message'=>'ساقلاش تامام'];
        }else{
            $data=['status'=>1,'message'=>'ساقلاش مەغلۇب بولدى!'];
        }
        $this->ajaxReturn($data);
    }
    //分别编辑
    public function edit($id)
    {
        $this->page_title="تەڭشەك تەھرىرلەش";
        $this->configs=M('configs')->where(array('id'=>$id))->find();
        $this->display();
    }
    //分别编辑完成后提交表单保存到数据库
    public function savehend()
    {
        if(!M('configs')->save(I('post.'))) $this->error('编辑失败！');
        $this->success('编辑成功了',U('Configs/index'),1);
    }

    //删除
    public function deleteConfig()
    {
        if(M('configs')->where(array('id'=>I('post.id')))->delete()){
            $data=['status'=>1,'message'=>'ئۆچۈرۈش تامام'];
        }
        else{
            $data=['status'=>1,'message'=>'ئۆچۈرۈش مەغلۇب بولدى'];
        }
        $this->ajaxReturn($data);
    }

}
